package com.zy.validator;


import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by zhaoyang on 2019/6/20.
 */
public class ValidatorUtil {
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    public static <T> ValidateResult validate(T obj){
        Map<String,StringBuffer> errorMap = null;
        Set<ConstraintViolation<T>> set = validator.validate(obj,Default.class);
        ValidateResult validateResult = new ValidateResult();
        if(set != null && set.size() >0 ){
            validateResult.setHasError(true);
            errorMap = new HashMap<String,StringBuffer>();
            String property = null;
            for(ConstraintViolation<T> cv : set){
                //这里循环获取错误信息，可以自定义格式
                property = cv.getPropertyPath().toString();
                if(errorMap.get(property) != null){
                    errorMap.get(property).append("," + cv.getMessage());
                }else{
                    StringBuffer sb = new StringBuffer();
                    sb.append(cv.getMessage());
                    errorMap.put(property, sb);
                }
            }
            StringBuilder sb = new StringBuilder();
            errorMap.forEach(( k, v)->{
                String v1 = v.toString();
                sb.append(k).append(":").append(v1);
            });
            validateResult.setErrMsg(sb.toString());
        }
        return validateResult;
    }

}
